﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyWeb.Business.System;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using MyWeb.Model;
using MyWeb.Model.Models;
using MyWeb.Repository.UnitOfWorks;
using MyWeb.Services;
using SqlSugar;
using StackExchange.Redis;
using static MyWeb.Extensions.ServiceExtensions.Swagger.CustomApiVersion;
using Role = MyWeb.Model.Models.Role;

namespace MyWeb.Api.Controllers.V1.System
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [CustomRoute(ApiVersions.V1)]
    public class RoleController : BaseController
    {
        private readonly IRoleService _roleService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="roleService"></param>
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult> Get()
        {

            var data = await _roleService.Query();
            return Success(data);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetPage(int page = 1,int pageSize = 50, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            RefAsync<int> total = 0;
            var data = await _roleService.GetPage(total, page, pageSize, key);
            int count = total.Value;
            return SuccessPage<Role>(page,count,pageSize,data);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> EditRole([FromBody]Role role)
        {
            var b = await _roleService.Edit(role);
            return b ? Success() : Failed();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddRole([FromBody]Role role)
        {
            var r = await _roleService.Add(role);
            return r>=1 ? Success() : Failed();
        }
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> RemoveRole(long id)
        {
            var r = await _roleService.Remove(id);
            return r ? Success() : Failed();
        }
    }
}
